test.js ➔ describe(ꞌunit testsꞌ)   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
cc 1
c 8
b 0
f 1
nc 1
nop 0
dl 0
loc 77
rs 8.9342

1 Function

Rating   Name   Duplication   Size   Complexity  
A test.js ➔ ... ➔ describe(ꞌhelpers.jsꞌ) 0 75 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* global describe, it */
2
3
const processFile = require('../dist/processFile.js').default
4
const {processText} = require('../dist/processFile.js')
5
const {findCode} = require('../dist/findCode.js')
6
const {findMdpInsert} = require('../dist/findMdpInsert.js')
7
const {findMdpCode} = require('../dist/findMdpCode.js')
8
const {replaceLineEndings} = require('../dist/helpers.js')
9
const {runCliCmd} = require('../dist/processFile.js')
10
const assert = require('assert')
11
const {exec} = require('child_process')
12
const fs = require('fs-extra')
13
const os = require('os')
14
15
const mdpLinkTests = [
16
  {name: 'Simple 1', text: '[>]: # (mdpInsert abc)\r\n12345\r\n[<]: #\r\n', result: {start: 0, length: 37, internalStart: 24, internalLength: 5, commandString: 'mdpInsert abc'}},
17
  {name: 'Simple 2', text: 't\n[> a]: # (mdpInsert abc)\r\n12345\r\n[< b]: #\r\n', result: {start: 2, length: 41, internalStart: 28, internalLength: 5, commandString: 'mdpInsert abc'}},
18
  {name: 'Simple 3', text: '[>]: # (mdpInsert a)\r\n[<]: #\r\n', result: {start: 0, length: 28, internalStart: 22, internalLength: -2, commandString: 'mdpInsert a'}},
19
  {name: 'Simple 4', text: '[>]: # (mdpInsert abc)\r\n12345\r\n[<]: #', result: {start: 0, length: 37, internalStart: 24, internalLength: 5, commandString: 'mdpInsert abc'}},
20
  {name: 'Simple 5 (invalid)', text: '[#]: # (mdpInsert abc)\r\n12345\r\n[<]: #\r\n', result: {start: -1}},
21
  {name: 'code fenced', text: '[>]: # (mdpInsert abc)\r\n12345\r\n`[<]`: #\r\n', result: {start: -1}},
22
  {name: 'embedded', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 76, internalStart: 24, internalLength: 44, commandString: 'mdpInsert abc'}},
23
  {name: 'embedded, 5 deep', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 193, internalStart: 24, internalLength: 161, commandString: 'mdpInsert abc'}},
24
  {name: 'embedded, too deep (6) - fails', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: -1}},
25
  {name: 'embedded, some fenced 1', text: '[>]: # (mdpInsert abc)\r\n12\r\n```\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n```\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n```\r\n[<]: #\r\n```\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 250, internalStart: 24, internalLength: 218, commandString: 'mdpInsert abc'}},
26
  {name: 'embedded, some fenced 2', text: '[>]: # (mdpInsert abc)\r\n12\r\n```\r\n[<]: #\r\n```\r\n[<]: #\r\n[<]: #\r\n', result: {start: 0, length: 52, internalStart: 24, internalLength: 20, commandString: 'mdpInsert abc'}},
27
  {name: 'embedded with following', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n345\r\n[<]: #\r\n[>]: # (mdpInsert xyz)\r\n', result: {start: 0, length: 76, internalStart: 24, internalLength: 44, commandString: 'mdpInsert abc'}}
28
]
29
30
const mdpCodeTests = [
31
  {name: 'Simple 1', text: '```json mdpInsert cat e.txt\r\ncode\r\n```', result: {start: 0, length: 38, internalStart: 29, internalLength: 4, commandString: 'json mdpInsert cat e.txt'}},
32
  {name: 'Fails mdpinsert spelt wrong', text: '```json mdpinsert cat e.txt\r\ncode\r\n```', result: {start: -1}},
33
  {name: 'Complex command line', text: 'text\r\n``` json   notes  mdpInsert cat e.txt --option1 sp --option2 \r\ncode\r\n```\r\n', result: {start: 6, length: 72, internalStart: 69, internalLength: 4, commandString: 'json   notes  mdpInsert cat e.txt --option1 sp --option2'}}
34
]
35
36
const fencedCodeTests = [
37
  {name: 'Simple 1', text: 'plain\r\n```\r\ncode\r\n```\r\nplain', result: {start: 7, length: 14, internalStart: 12, internalLength: 4, commandString: ''}},
38
  {name: 'Simple 2', text: 'plain\n```\ncode\n```\nplain', result: {start: 6, length: 12, internalStart: 10, internalLength: 4, commandString: ''}},
39
  {name: 'Example 88', text: '```\n<\n >\n```', result: {start: 0, length: 12, internalStart: 4, internalLength: 4, commandString: ''}},
40
  {name: 'Example 89', text: '~~~\n<\n >\n~~~', result: {start: 0, length: 12, internalStart: 4, internalLength: 4, commandString: ''}},
41
  {name: 'Example 90', text: '``\nfoo\n``', result: {start: 0, length: 9, internalStart: 2, internalLength: 5, commandString: ''}},
42
  {name: 'Example 91', text: '```\naaa\n~~~\n```', result: {start: 0, length: 15, internalStart: 4, internalLength: 7, commandString: ''}},
43
  {name: 'Example 92', text: '~~~\naaa\n```\n~~~', result: {start: 0, length: 15, internalStart: 4, internalLength: 7, commandString: ''}},
44
  {name: 'Example 93', text: '````\naaa\n```\n``````', result: {start: 0, length: 19, internalStart: 5, internalLength: 7, commandString: ''}},
45
  {name: 'Example 94', text: '~~~~\naaa\n~~~\n~~~~', result: {start: 0, length: 17, internalStart: 5, internalLength: 7, commandString: ''}},
46
  {name: 'Example 95', text: '```', result: {start: 0, length: 3, internalStart: 3, internalLength: 0, commandString: ''}},
47
  {name: 'Example 96', text: '`````\n\n```\naaa', result: {start: 0, length: 14, internalStart: 6, internalLength: 8, commandString: ''}},
48
  {name: 'Example 97', text: '> ```\n> aaa\n\nbbb', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
49
  {name: 'Example 97a', text: '> ```\n> aaa\n> \n> bbb', result: {start: 0, length: 20, internalStart: 6, internalLength: 14, commandString: ''}},
50
  {name: 'Example 97b', text: '> ```\r\n> aaa\r\n\r\nbbb', result: {start: 0, length: 12, internalStart: 7, internalLength: 5, commandString: ''}},
51
  {name: 'Example 97c', text: '> ```\r\n> aaa\r\n> \r\n> bbb', result: {start: 0, length: 23, internalStart: 7, internalLength: 16, commandString: ''}},
52
  {name: 'Example 97d', text: '>```a\r\n>\r\nbbb', result: {start: 0, length: 8, internalStart: 7, internalLength: 1, commandString: 'a'}},
53
  {name: 'Example 98', text: '```\n\n  ```', result: {start: 0, length: 10, internalStart: 4, internalLength: 0, commandString: ''}},
54
  {name: 'Example 99', text: '```\n```', result: {start: 0, length: 7, internalStart: 4, internalLength: -1, commandString: ''}},
55
  {name: 'Example 100', text: ' ```\n aaa\naaa\n```', result: {start: 0, length: 17, internalStart: 5, internalLength: 8, commandString: ''}},
56
  {name: 'Example 101', text: '  ```\naaa\n  aaa\naaa\n  ```', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
57
  {name: 'Example 101b', text: '  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
58
  {name: 'Example 101c', text: '  ```\naaa\n  aaa\naaa\n  ```\n', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
59
  {name: 'Example 101d', text: '\r\n  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 2, length: 25, internalStart: 8, internalLength: 13, commandString: ''}},
60
  {name: 'Example 101e', text: '\n  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 1, length: 25, internalStart: 7, internalLength: 13, commandString: ''}},
61
  {name: 'Example 102', text: '   ```\n   aaa\n    aaa\n   ```', result: {start: 0, length: 28, internalStart: 7, internalLength: 14, commandString: ''}},
62
  {name: 'Example 103', text: '    ```\n    aaa\n    ```', result: {start: 0, length: 23, internalStart: 0, internalLength: 23, commandString: ''}},
63
  {name: 'Example 104', text: '```\naaa\n  ```', result: {start: 0, length: 13, internalStart: 4, internalLength: 3, commandString: ''}},
64
  {name: 'Example 105', text: '   ```\naaa\n  ```', result: {start: 0, length: 16, internalStart: 7, internalLength: 3, commandString: ''}},
65
  {name: 'Example 106', text: '```\naaa\n    ```', result: {start: 0, length: 15, internalStart: 4, internalLength: 11, commandString: ''}},
66
  {name: 'Example 107', text: '``` ```\naaa', result: {start: 0, length: 7, internalStart: 3, internalLength: 1, commandString: ''}},
67
  {name: 'Example 108', text: '~~~~~~\naaa\n~~~ ~~', result: {start: 0, length: 17, internalStart: 7, internalLength: 10, commandString: ''}},
68
  {name: 'Example 109', text: 'foo\n```\nbar\n```\nbaz', result: {start: 4, length: 11, internalStart: 8, internalLength: 3, commandString: ''}},
69
  {name: 'Example 110', text: 'foo\n---\n~~~\nbar\n~~~\n# baz', result: {start: 8, length: 11, internalStart: 12, internalLength: 3, commandString: ''}},
70
  {name: 'Example 111', text: '```ruby\ndef foo(x)\n  return 3\nend\n```', result: {start: 0, length: 37, internalStart: 8, internalLength: 25, commandString: 'ruby'}},
71
  {name: 'Example 112', text: '~~~~    ruby startline=3 $%@#$\r\ndef foo(x)\r\n  return 3\r\nend\r\n~~~~~~~', result: {start: 0, length: 68, internalStart: 32, internalLength: 27, commandString: 'ruby startline=3 $%@#$'}},
72
  {name: 'Example 113', text: '````;\r\n````', result: {start: 0, length: 11, internalStart: 7, internalLength: -2, commandString: ';'}},
73
  {name: 'Example 114', text: '``` aa ```\r\nfoo', result: {start: 0, length: 10, internalStart: 3, internalLength: 4, commandString: ''}},
74
  {name: 'Example 115', text: '```\r\n``` aaa\r\n```', result: {start: 0, length: 17, internalStart: 5, internalLength: 7, commandString: ''}},
75
  {name: 'Example 324', text: '`foo`', result: {start: 0, length: 5, internalStart: 1, internalLength: 3, commandString: ''}},
76
  {name: 'Example 325', text: '`` foo ` bar  ``', result: {start: 0, length: 16, internalStart: 2, internalLength: 12, commandString: ''}},
77
  {name: 'Example 326', text: '` `` `', result: {start: 0, length: 6, internalStart: 1, internalLength: 4, commandString: ''}},
78
  {name: 'Example 327', text: '``\r\nfoo\r\n``', result: {start: 0, length: 11, internalStart: 2, internalLength: 7, commandString: ''}},
79
  {name: 'Example 328', text: '`foo   bar\r\n  baz`', result: {start: 0, length: 18, internalStart: 1, internalLength: 16, commandString: ''}},
80
  {name: 'Example 329', text: '`a b`', result: {start: 0, length: 5, internalStart: 1, internalLength: 3, commandString: ''}},
81
  {name: 'Example 330', text: '`foo `` bar`', result: {start: 0, length: 12, internalStart: 1, internalLength: 10, commandString: ''}},
82
  {name: 'Example 331', text: '`foo\\`bar`', result: {start: 0, length: 6, internalStart: 1, internalLength: 4, commandString: ''}},
83
  {name: 'Example 332', text: '*foo`*`', result: {start: 4, length: 3, internalStart: 5, internalLength: 1, commandString: ''}},
84
  {name: 'Example 333', text: '[not a `link](/foo`)', result: {start: 7, length: 12, internalStart: 8, internalLength: 10, commandString: ''}},
85
  {name: 'Example 334', text: '`<a href="`">`', result: {start: 0, length: 11, internalStart: 1, internalLength: 9, commandString: ''}},
86
  {name: 'Example 336', text: '`<http://foo.bar.`baz>`', result: {start: 0, length: 18, internalStart: 1, internalLength: 16, commandString: ''}},
87
  {name: 'Example 338', text: '```foo``', result: {start: -1}},
88
  {name: 'Example 339', text: '`foo', result: {start: -1}},
89
  {name: 'Example 340', text: '`foo``bar``', result: {start: 4, length: 7, internalStart: 6, internalLength: 3, commandString: ''}},
90
  {name: 'block and code 1', text: '>```\r\n>code\r\ntext', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
91
  {name: 'block and code 1b', text: '>```\r\n>code\ntext', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
92
  {name: 'block and code 2', text: '>```\r\n>code\r\ntext```\r\n', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
93
  {name: 'block and code 3', text: '>```\r\n>code\r\n>```\r\ntext', result: {start: 0, length: 17, internalStart: 6, internalLength: 5, commandString: ''}}
94
]
95
96
const lineEndingTests = [
97
  {name: '1', text: '\nabc\r\nabc\n\n', resultCRLF: '\r\nabc\r\nabc\r\n\r\n', resultLF: '\nabc\nabc\n\n'},
98
  {name: '2', text: ' \n\nabc\n\rabc\n ', resultCRLF: ' \r\n\r\nabc\r\n\rabc\r\n ', resultLF: ' \n\nabc\n\rabc\n '},
99
  {name: '3', text: '\r\nabc\nabc\r\n', resultCRLF: '\r\nabc\r\nabc\r\n', resultLF: '\nabc\nabc\n'},
100
  {name: '4', text: ' \r\n\r\nabc\r\n\rabc\r\n ', resultCRLF: ' \r\n\r\nabc\r\n\rabc\r\n ', resultLF: ' \n\nabc\n\rabc\n '}
101
]
102
103
const processTextTests = [
104
  {
105
    name: 'Simple 1',
106
    text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
107
    clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
108
    full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
109
    full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
110
  }, {
111
    name: 'Simple 1a',
112
    text: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n\r\n12345\r\n[<]: #\r\n',
113
    clear: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n[<]: #\r\n',
114
    full_win: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n```\r\nabc\r\n```\r\n[<]: #\r\n',
115
    full_linux: '[>]: # (mdpInsert ``` cat test/docs/abc2.txt)\r\n```\r\nabc\r\n```\r\n[<]: #\r\n'
116
  }, {
117
    name: 'mdpInsert command not present',
118
    text: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
119
    clear: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
120
    full_win: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
121
    full_linux: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n'
122
  }, {
123
    name: 'Command Line invalid',
124
    text: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
125
    clear: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\n[<]: #\r\n',
126
    full_win: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\nERROR: Command failed: catt test/docs/abc.txt\r\n\'catt\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n[<]: #\r\n',
127
    full_linux: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\nERROR: Command failed: catt test/docs/abc.txt\r\n/bin/sh: 1: catt: not found\r\n[<]: #\r\n'
128
  }, {
129
    name: 'Surrounded',
130
    text: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nold text\r\n[<]: #\r\nOther text',
131
    clear: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\nOther text',
132
    full_win: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\nOther text',
133
    full_linux: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\nOther text'
134
  }, {
135
    name: 'Empty',
136
    text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n\r\n[<]: #\r\n',
137
    clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
138
    full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
139
    full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
140
  }, {
141
    name: 'Blank Line',
142
    text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
143
    clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
144
    full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
145
    full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
146
  }, {
147
    name: 'LF only',
148
    text: '[>]: # (mdpInsert cat test/docs/abc.txt)\n[<]: #\n',
149
    clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\n[<]: #\n',
150
    full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\nabc\n[<]: #\n',
151
    full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\nabc\n[<]: #\n'
152
  }, {
153
    name: 'add code fence 1',
154
    text: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n[<]: #\n',
155
    clear: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n[<]: #\n',
156
    full_win: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n```json\nabc\n```\n[<]: #\n',
157
    full_linux: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n```json\nabc\n```\n[<]: #\n'
158
  }, {
159
    name: 'add code fence 2',
160
    text: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n[<]: #\r\n',
161
    clear: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n[<]: #\r\n',
162
    full_win: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n```json\r\nabc\r\n```\r\n[<]: #\r\n',
163
    full_linux: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n```json\r\nabc\r\n```\r\n[<]: #\r\n'
164
  }, {
165
    name: 'add code fence 3',
166
    text: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n[<]: #\r\n',
167
    clear: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n[<]: #\r\n',
168
    full_win: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n~~~~json\r\nabc\r\n~~~~\r\n[<]: #\r\n',
169
    full_linux: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n~~~~json\r\nabc\r\n~~~~\r\n[<]: #\r\n'
170
  }, {
171
    name: 'Two replacements',
172
    text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
173
    clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
174
    full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
175
    full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
176
  }, {
177
    name: 'Code Fence 1',
178
    text: '```json mdpInsert cat test/docs/abc.txt\r\nxyz\r\ncode\r\n```',
179
    clear: '```json mdpInsert cat test/docs/abc.txt\r\n```',
180
    full_win: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```',
181
    full_linux: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```'
182
  }, {
183
    name: 'Code Fence 2',
184
    text: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\n```',
185
    clear: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\n```',
186
    full_win: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```',
187
    full_linux: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```'
188
  }, {
189
    name: 'Code Fence and link',
190
    text: '```json mdpInsert cat test/docs/abc.txt\r\nxyz\r\ncode\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
191
    clear: '```json mdpInsert cat test/docs/abc.txt\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
192
    full_win: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
193
    full_linux: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
194
  }, {
195
    name: 'Missing File',
196
    text: '[>]: # (mdpInsert cat file/not/present.txt)\r\n12345\r\n[<]: #\r\n',
197
    clear: '[>]: # (mdpInsert cat file/not/present.txt)\r\n[<]: #\r\n',
198
    full_win: '[>]: # (mdpInsert cat file/not/present.txt)\r\nERROR: Command failed: type file\\not\\present.txt\r\nThe system cannot find the path specified.\r\n[<]: #\r\n',
199
    full_linux: '[>]: # (mdpInsert cat file/not/present.txt)\r\nERROR: Command failed: cat file/not/present.txt\r\ncat: file/not/present.txt: No such file or directory\r\n[<]: #\r\n'
200
  }
201
]
202
203
describe('unit tests', function () {
204
  describe('helpers.js', function () {
205
    describe('replaceLineEndings', function () {
206
      for (let i = 0; i < lineEndingTests.length; i++) {
207
        it(lineEndingTests[i].name + ' CRLF', function () {
208
          assert.equal(replaceLineEndings(lineEndingTests[i].text, true), lineEndingTests[i].resultCRLF)
209
        })
210
        it(lineEndingTests[i].name + ' LF', function () {
211
          assert.equal(replaceLineEndings(lineEndingTests[i].text, false), lineEndingTests[i].resultLF)
212
        })
213
      }
214
    })
215
    describe('findCode', function () {
216
      for (let i = 0; i < fencedCodeTests.length; i++) {
217
        it(fencedCodeTests[i].name, function () {
218
          let r = findCode(fencedCodeTests[i].text)
219
          delete r.info
220
          assert.deepEqual(r, fencedCodeTests[i].result)
221
        })
222
      }
223
    })
224
    describe('findMdpInsertBlocks', function () {
225
      for (let i = 0; i < mdpLinkTests.length; i++) {
226
        it(mdpLinkTests[i].name, function () {
227
          let r = findMdpInsert(mdpLinkTests[i].text)
228
          delete r.info
229
          assert.deepEqual(r, mdpLinkTests[i].result)
230
        })
231
      }
232
    })
233
    describe('findMdpCode', function () {
234
      for (let i = 0; i < mdpCodeTests.length; i++) {
235
        it(mdpCodeTests[i].name, function () {
236
          let r = findMdpCode(mdpCodeTests[i].text)
237
          delete r.info
238
          assert.deepEqual(r, mdpCodeTests[i].result)
239
        })
240
      }
241
    })
242
    describe('runCliCmd', function () {
243
      it('Simple', function () {
244
        assert.equal(runCliCmd('cat abc.txt', 'test/docs').toString(), 'abc')
245
      })
246
      it('invalid', function () {
247
        if (os.platform() === 'win32') {
248
          assert.equal(runCliCmd('catt abc.txt', 'test/docs').toString(), 'ERROR: Command failed: catt abc.txt\n\'catt\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n')
249
        } else {
250
          assert.equal(runCliCmd('catt abc.txt', 'test/docs').toString(), 'ERROR: Command failed: catt abc.txt\n/bin/sh: 1: catt: not found\n')
251
        }
252
      })
253
    })
254
    describe('processText', function () {
255
      for (let i = 0; i < processTextTests.length; i++) {
256
        it(processTextTests[i].name + ' --clear', function () {
257
          let r = processText(processTextTests[i].text, true, process.cwd())
258
          assert.equal(r, processTextTests[i].clear)
259
        })
260
        it(processTextTests[i].name + ' full', function () {
261
          let r = processText(processTextTests[i].text, false, process.cwd())
262
          if (os.platform() === 'win32') {
263
            assert.equal(r, processTextTests[i].full_win)
264
          } else {
265
            assert.equal(r, processTextTests[i].full_linux)
266
          }
267
        })
268
      }
269
    })
270
    describe('processFile.js', function () {
271
      it('reference to processFile.js OK', function () {
272
        assert(processFile)
273
      })
274
      it('process file against missing file should error', function () {
275
        assert.throws(function () { processFile('not a file') }, /file not found/)
276
      })
277
    })
278
  })
279
})
280
281
describe('mdprepare', function () {
282
  it('mdprepare --help works', function (done) {
283
    this.timeout(8000)
284
    exec('mdprepare --help', function (error, stdout) {
285
      assert.ifError(error)
286
      assert.equal(stdout.toString(), fs.readFileSync('./test/help.txt').toString())
287
      done()
288
    })
289
  })
290
291
  it('fires and returns "processed x files message (1)"', function (done) {
292
    this.timeout(8000)
293
    exec('mdprepare --ignore=**/*.md', function (error, stdout) {
294
      assert.ifError(error)
295
      assert.notEqual(stdout.toString().indexOf('processed 0 files'), -1)
296
      done()
297
    })
298
  })
299
300
  it('fires and returns "processed x files message (2)"', function (done) {
301
    this.timeout(8000)
302
    exec('mdprepare fred', function (error, stdout) {
303
      assert.ifError(error)
304
      assert.notEqual(stdout.toString().indexOf('processed 0 files'), -1)
305
      done()
306
    })
307
  })
308
309
  it('fires and returns error if directory not found', function (done) {
310
    this.timeout(8000)
311
    exec('mdprepare //', function (error, stdout, stderr) {
312
      assert.ifError(error)
313
      assert.notEqual(stderr.toString(), '')
314
      done()
315
    })
316
  })
317
318
  it('fires and processes multiple files from wildcard', function (done) {
319
    this.timeout(8000)
320
    exec('mdprepare ./test/multiFile/*.md', function (error, stdout) {
321
      assert.ifError(error)
322
      assert.notEqual(stdout.toString().indexOf('processed 3 files'), -1)
323
      done()
324
    })
325
  })
326
})
327
328
describe('mdprepare test files', function () {
329
  fs.emptyDirSync('./test/preparedClear')
330
  fs.copySync('./test/docs', './test/preparedClear')
331
  fs.emptyDirSync('./test/preparedFull')
332
  fs.copySync('./test/docs', './test/preparedFull')
333
  var files = fs.readdirSync('./test/docs')
334
  for (let i = 0; i < files.length; i++) {
335
    if (files[i].substr(-3) === '.md') {
336
      it('/test/docs/' + files[i] + ' is prepared correctly', function (done) {
337
        this.timeout(8000)
338
        exec('mdprepare ./test/preparedFull/' + files[i], function (error) {
339
          assert.ifError(error)
340
          assert.equal(fs.readFileSync('./test/preparedFull/' + files[i], 'utf8'), fs.readFileSync('./test/preparedFull/' + files[i] + '.full', 'utf8'))
341
          done()
342
        })
343
      })
344
      it('/test/docs/' + files[i] + ' is cleared correctly', function (done) {
345
        this.timeout(8000)
346
        exec('mdprepare ./test/preparedClear/' + files[i] + ' --clear', function (error) {
347
          assert.ifError(error)
348
          assert.equal(fs.readFileSync('./test/preparedClear/' + files[i], 'utf8'), fs.readFileSync('./test/preparedClear/' + files[i] + '.clear', 'utf8'))
349
          done()
350
        })
351
      })
352
    }
353
  }
354
  it('test working with read only file - should error', function (done) {
355
    this.timeout(8000)
356
    fs.writeFileSync('./test/preparedClear/readonly.md', '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n')
357
    fs.chmodSync('./test/preparedClear/readonly.md', 0o444) // make file read only
358
    exec('mdprepare ./test/preparedClear/readonly.md', function (error, stdout, stderr) {
359
      assert.ifError(error)
360
      assert.notEqual(stderr.toString(), '')
361
      done()
362
    })
363
  })
364
})
365